#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

#define PORT 8888

int main()
{
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
    {
        perror("socket failed");
        exit(1);
    }
    struct sockaddr_in server_addr;
    char *message = "Hello Server";

    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(PORT);
    //把 "127.0.0.1" 转换成二进制 IP（例如 0x7f000001），然后填进 server_addr.sin_addr 中，准备用于 connect()
    inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);

    if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
    {
        perror("connect failed");
        close(sock);
        exit(1);
    }

    if (send(sock, message, strlen(message), 0) < 0)
    {
        perror("send failed");
    }
    else
    {
        printf("Sent message to server: %s\n", message);
    }
    
    close(sock);
    return 0;
}
